package com.ys.peony.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ys.peony.R;
import com.ys.peony.app.AppManager;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.base.BaseModel;
import com.ys.peony.base.contract.BasicActivity;
import com.ys.peony.base.contract.LoginContract;
import com.ys.peony.database.DataBaseUtil;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespBean;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.LoginModel;
import com.ys.peony.model.UserModel;
import com.ys.peony.presenter.LoginPresenter;
import com.ys.peony.rolecontrol.entity.Component;
import com.ys.peony.rolecontrol.entity.Info;
import com.ys.peony.rolecontrol.entity.Layout;
import com.ys.peony.rolecontrol.entity.RoleConfig;
import com.ys.peony.rolecontrol.presenter.RoleControlPresenter;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.StringUtils;
import com.ys.peony.utils.ToastUtils;
import com.zhitongyun.publics.greendao.InfoDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * describe:登录
 * date:2017/5/3 22:15
 * version V1.0
 */
public class LoginActivity extends BasicActivity<LoginContract.iLoginView, LoginPresenter> implements LoginContract.iLoginView, View.OnClickListener {

    @BindView(R.id.login_account)
    EditText loginAccount;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @BindView(R.id.url_et)
    EditText urlET;
    @BindView(R.id.set_btn)
    Button setBtn;
    @BindView(R.id.linearLayout_login)
    LinearLayout linearLayoutLogin;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

    }

    @Override
    protected int getContentResId() {
        return R.layout.login_activity;
    }

    @Override
    protected void initView() {
        /*if(!AppManager.getAppManager().isNewestApk(this)) {
            return;
        }*/
        btnLogin.setOnClickListener(this);
        urlET.setText(SharePreferencesUtils.getInstance().getUrl());
        setBtn.setOnClickListener(this);
        linearLayoutLogin.setOnClickListener(this);
        if (presenter.checkCurUserValid()) {
            gotoMain();
        }
        UserModel userModel = SharePreferencesUtils.getInstance().getUserModel();
        if (null != userModel) {
            loginAccount.setText(userModel.getAccount());
            loginAccount.setSelection(loginAccount.getText().length());
        }
        tvVersion.setText(CommonUtils.getVersion());
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                presenter.login(loginAccount.getText().toString().trim(),
                        loginPwd.getText().toString().trim());
                break;
            case R.id.set_btn:
                SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_HTTP_BASEURL, StringUtils.trimNull(urlET.getText().toString()));
                break;
            case R.id.linearLayout_login:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                break;
            default:
                break;
        }
    }

    public void gotoMain() {
        skipToTargetPage(MainActivity.class, null, SwipeDirect.DIRECT_LEFT);
        finish();
    }

    @Override
    protected LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void onLoginResult(int code) {

    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(msg);
    }
}

